package cn.exrick.xboot.vip.controller;

import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.vip.entity.VipCard;
import cn.exrick.xboot.vip.entity.VipCardDeposit;
import cn.exrick.xboot.vip.service.IVipCardDepositService;
import cn.exrick.xboot.vip.service.IVipCardService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
* <p>
    * 会员卡充值  前端控制器
    * </p>
*
* @author yuhaifei
* @since 2022-10-09
*/
@RestController
@RequestMapping("/xboot/vipcarddeposit")
public class VipCardDepositController {


    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private IVipCardService vipCardService;

    @Autowired
    private IVipCardDepositService vipCardDepositService;

    /**
     * 充值
     * @param deposit
     * @return
     */
    @Transactional
    @PostMapping("addDeposit")
    public Result addDeposit(VipCardDeposit deposit){

        // 会员卡余额
        VipCard vipCard = vipCardService.getOne(new QueryWrapper<VipCard>().eq("card_id", deposit.getCardId()));
        if(deposit.getType() == 0){//充值
            vipCard.setCardMoney(vipCard.getCardMoney() + deposit.getMoney() + deposit.getMoneyOther());
        }else if(deposit.getType() == 1){//退款
            vipCard.setCardMoney(vipCard.getCardMoney() - deposit.getMoney());
        }
        vipCardService.updateById(vipCard);
        // 插入一条充值记录
        deposit.setCreateTime(new Date());
        deposit.setCreateUser(securityUtil.getCurrMember().getId());
        vipCardDepositService.save(deposit);
        return ResultUtil.data(vipCard);
    }

    /**
     * 分页获取充值记录
     * @param pageVo
     * @param deposit
     * @return
     */
    @GetMapping("page")
    public Result<IPage<VipCardDeposit>> page(PageVo pageVo, VipCardDeposit deposit){
        Page page = PageUtil.initMpPage(pageVo);
        QueryWrapper<VipCardDeposit> wrapper = new QueryWrapper<VipCardDeposit>();
        this.setWapper(deposit, wrapper);
        IPage<VipCardDeposit> ipage = vipCardDepositService.page(page, wrapper);
        return new ResultUtil<IPage<VipCardDeposit>>().setData(ipage);

    }

    public void setWapper(VipCardDeposit deposit, QueryWrapper wrapper){
        if(StrUtil.isNotBlank(deposit.getCardId())){
            wrapper.like("card_id", deposit.getCardId());
        }

    }

}
